require('@assets/css/login.css')
const {
	$U_GetItem,
	$U_UserInfo,
	$U_LocalUserInfo,
} = require('@utils/storage')
const {
	loginMethod,
} = require('./login-methods')

module.exports = function() {
	let {
		loginComp,
	} = $['components-jq']

	loginComp.formComp = $("#login-form")
	loginComp.accountComp = $("#login-account")
	loginComp.passwordComp = $("#login-password")
	loginComp.submitComp = $("#login-submit")
	loginComp.store = {
		isLogin: false
	}

	let localUserInfo = $U_LocalUserInfo()
	loginComp.accountComp.textbox({
		prompt: '请输入用户名',
		iconCls: 'icon-man',
		height: 40,
		required: true,
		value: localUserInfo.account || '',
	})

	loginComp.passwordComp.passwordbox({
		prompt: '请输入密码',
		showEye: true,
		height: 40,
		lastDelay: 1000,
		required: true,
		value: localUserInfo.orgPassword || '',
	})

	loginComp.submitComp.linkbutton({
		width: 200,
		height: 30,
		text: '登录',
		onClick() {
			loginMethod.call(loginComp);
		},
	})

	// 回车登录
	document.onkeydown = function(e) {
		let currKey = e.keyCode || e.which || e.charCode;
		if (currKey == 13 && !loginComp.store.isLogin) {
			// 取消密码框焦点
			$('[placeholder="请输入密码"]').get(0).blur()
			setTimeout(function() {
				loginMethod.call(loginComp);
			}, 200)
		}
	}
}